//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Client.h"
#include "Ws2tcpip.h"
//---------------------------------------------------------------------------
#pragma link "Ws2_32.lib"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm9 *Form9;
//---------------------------------------------------------------------------

#define DEFAULT_BUFLEN 1024*1024
#define DEFAULT_PORT "27015"

__fastcall TForm9::TForm9(TComponent* Owner)
	: TForm(Owner)
{
	fWSAStartupRet = WSAStartup(MAKEWORD(2,2), &fWSAData);
}
//---------------------------------------------------------------------------

void __fastcall TForm9::Button1Click(TObject *Sender)
{
	WSADATA wsaData;
	int iResult;
    SOCKET ConnectSocket = INVALID_SOCKET;
    ADDRINFOA *result = NULL,
                    *ptr = NULL,
					hints;
	char* ip = "192.168.200.110";
	char *recvbuf = char(DEFAULT_BUFLEN);
	int recvbuflen = DEFAULT_BUFLEN;

    if (fWSAStartupRet != 0)
		return ;
	ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (ConnectSocket == INVALID_SOCKET) {
 		WSACleanup();
		return;
    }
	ZeroMemory(&hints, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	// Resolve the server address and port
	iResult = getaddrinfo(ip, DEFAULT_PORT, &hints, &result);
	if ( iResult != 0 ) {
 		WSACleanup();
		return ;
	}

    // Attempt to connect to an address until one succeeds
    for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
        // Create a SOCKET for connecting to server
        ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
            ptr->ai_protocol);
        if (ConnectSocket == INVALID_SOCKET) {
	 		freeaddrinfo(result);
            WSACleanup();
            return;
        }
		// Connect to server.
        iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
		if (iResult == SOCKET_ERROR) {
			closesocket(ConnectSocket);
			ConnectSocket = INVALID_SOCKET;
            continue;
        }
        break;
	}
	freeaddrinfo(result);
    if (ConnectSocket == INVALID_SOCKET) {
	  	WSACleanup();
        return ;
	}
	// Send an initial buffer
    iResult = send(ConnectSocket, AnsiString(Edit1->Text).c_str(), Edit1->Text.Length(), 0);
    if (iResult == SOCKET_ERROR) {
		//printf("send failed: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
	   	WSACleanup();
        return  ;
	}
    /*
    // shutdown the connection since no more data will be sent
	iResult = shutdown(ConnectSocket, SD_SEND);
	if (iResult == SOCKET_ERROR) {
		closesocket(ConnectSocket);
	    WSACleanup();
	    return  ;
	}
    // Receive until the peer closes the connection
    do {
        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
        if ( iResult > 0 )
            printf("Bytes received: %d\n", iResult);
        else if ( iResult == 0 )
            printf("Connection closed\n");
        else
            printf("recv failed: %d\n", WSAGetLastError());

	} while( iResult > 0 );
	//cleanup
	closesocket(ConnectSocket);
	WSACleanup();
   	*/
    return  ;
}
//---------------------------------------------------------------------------
